//
//  JWAlarmListTableViewController.m
//  SiqiCalendar
//
//  Created by sunjiawen on 2018/4/2.
//  Copyright © 2018年 sjw. All rights reserved.
//

#import "JWAlarmListTableViewController.h"
#import "JWAlarmClockListViewModel.h"
#import "JWAlarmListCell.h"
#import "UIScrollView+EmptyDataSet.h"
#import "JWAlarmClockSettingViewController.h"

@interface JWAlarmListTableViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic, strong)JWAlarmClockListViewModel *viewModel;
@property(nonatomic, strong)NSMutableArray *dataSource;
@end

static NSString *const kAlarmCellID = @"AlarmListCellId";

@implementation JWAlarmListTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    
    UIBarButtonItem *addAlarmBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAlarmBarButtonItemAction:)];
    
    self.navigationItem.rightBarButtonItems = @[addAlarmBarButtonItem,self.editButtonItem];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
}

#pragma mark - Actions
- (IBAction)leftBarButtonAction:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addAlarmBarButtonItemAction:(UIBarButtonItem *)addAlarmBarButtonItem {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"JWAlarm" bundle:nil];
    if (storyboard) {
        JWAlarmClockSettingViewController *vc = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([JWAlarmClockSettingViewController class])];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JWAlarmListCell *cell = (JWAlarmListCell *)[tableView dequeueReusableCellWithIdentifier:kAlarmCellID forIndexPath:indexPath];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.dataSource removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

#pragma mark - DZNEmptyDataSetSource Methods
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"nodata-2"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = kNoDataText;
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: JWConfigShareInstance.noDataTextFont,
                                 NSForegroundColorAttributeName: JWConfigShareInstance.noDataTextColor,
                                 NSParagraphStyleAttributeName: paragraphStyle};
    
    return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}


#pragma mark - DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return NO;
}


- (JWAlarmClockListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[JWAlarmClockListViewModel alloc]init];
    }
    return _viewModel;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
